package com.imax.center.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.imax.center.common.Result;
import com.imax.center.domain.HomeCameraDomain;
import com.imax.center.domain.UpdateCameraOrderDomain;
import com.imax.center.domain.UserInfoDomain;
import com.imax.center.service.H3yunService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Api(tags = "H3yun接口")
@RestController
@RequestMapping("/api/h3yun")
public class H3yunController {

    @Autowired
    private H3yunService h3yunService;

    @ApiOperation(value = "获取VIP等级列表", notes = "从H3yun获取VIP等级列表")
    @GetMapping("/vip-levels")
    public Result<JSONArray> getVipLevels() {
        return h3yunService.getVipLevels();
    }

    @ApiOperation(value = "获取系统配置", notes = "获取系统配置")
    @GetMapping("/getCommonConfig")
    public Result<JSONArray> getCommonConfig() {
        return h3yunService.getCommonConfig();
    }

    @ApiOperation(value = "传感器列表", notes = "传感器列表")
    @GetMapping("/getSensorDeviceList")
    public Result<JSONArray> getSensorDeviceList(HttpServletRequest request) {
        String userInfoStr = (String) request.getAttribute("userInfo");
        UserInfoDomain userInfo = Objects.requireNonNull(JSON.parseObject(userInfoStr, UserInfoDomain.class));
        return h3yunService.getSensorDeviceList(userInfo.getPowerCode(), userInfo.getUnitId());
    }

    @ApiOperation(value = "预警事件删除图片(临时)列表", notes = "预警事件删除图片(临时)列表")
    @GetMapping("/getEventPicTempList")
    public Result<JSONArray> getEventPicTempList() {
        return h3yunService.getEventPicTempList();
    }

    @ApiOperation(value = "获取通道列表", notes = "获取通道列表")
    @GetMapping("/getChannelList")
    public Result<JSONArray> getChannelList(HttpServletRequest request) {
        String userInfoStr = (String) request.getAttribute("userInfo");
        UserInfoDomain userInfo = Objects.requireNonNull(JSON.parseObject(userInfoStr, UserInfoDomain.class));
        return h3yunService.queryChannelInfo(null, userInfo.getPowerCode(), userInfo.getUnitId());
    }

    @ApiOperation(value = "获取主监控点列表", notes = "获取主监控点列表")
    @GetMapping("/getHomeChannelList")
    public Result<JSONArray> getHomeChannelList(String areaCode, HttpServletRequest request) {
        String userInfoStr = (String) request.getAttribute("userInfo");
        UserInfoDomain userInfo = Objects.requireNonNull(JSON.parseObject(userInfoStr, UserInfoDomain.class));
        return h3yunService.getHomeChannelList(userInfo.getPowerCode(), areaCode);
    }

    @ApiOperation(value = "更新主监控点排序", notes = "更新主监控点排序")
    @PostMapping("/updateHomeCameraOrder")
    public Result<String> updateHomeCameraOrder(@RequestBody List<UpdateCameraOrderDomain> list) {
        return h3yunService.updateHomeCameraOrder(list);
    }

    @ApiOperation(value = "获取送检清单列表", notes = "获取送检清单列表")
    @GetMapping("/getCheckList")
    public Result<JSONArray> getCheckList(HttpServletRequest request) {
        String userInfoStr = (String) request.getAttribute("userInfo");
        UserInfoDomain userInfo = Objects.requireNonNull(JSON.parseObject(userInfoStr, UserInfoDomain.class));
        return h3yunService.getCheckList(userInfo.getPowerCode(), userInfo.getUnitId());
    }
    
    @ApiOperation(value = "根据设备ID查询设备信息", notes = "查询氚云平台上指定ID的设备信息")
    @GetMapping("/device")
    public Result<JSONObject> getDeviceById(
            @ApiParam(value = "设备ID", required = true) @RequestParam String deviceId) {
        log.info("根据设备ID查询设备信息: {}", deviceId);
        
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("DeviceID", deviceId);
        
        Result<JSONArray> result = h3yunService.queryDeviceInfo(queryParams);
        log.info("设备查询结果: {}", result);
        
        if (result.getData() == null || result.getData().isEmpty()) {
            return Result.error("未找到指定ID的设备信息");
        }
        
        // 只返回第一条记录
        return Result.success(result.getData().getJSONObject(0), "查询设备信息成功");
    }

    @ApiOperation(value = "批量删除传感器事件数据(JSON方式)")
    @PostMapping("/delete-sensor-events-json")
    public Result<JSONObject> deleteSensorEventDataJson(
            @ApiParam(value = "命令编码CN", required = false) @RequestParam(required = false) String cn) {
        return h3yunService.queryAndBatchDelSensorEventData(cn);
    }

    @ApiOperation(value = "查询客户列表信息")
    @GetMapping("/queryChildrenOrgList")
    public Result<JSONArray> queryChildrenOrgList(HttpServletRequest request) {
        String userInfoStr = (String) request.getAttribute("userInfo");
        UserInfoDomain userInfo = Objects.requireNonNull(JSON.parseObject(userInfoStr, UserInfoDomain.class));
        return h3yunService.queryChildrenOrgList(null, userInfo.getPowerCode(), userInfo.getUnitId());
    }

    @ApiOperation(value = "查询行政区划树")
    @GetMapping("/queryRegionTree")
    public Result<JSONObject> queryRegionTree(@ApiParam(value = "parentCode", required = true) @RequestParam String parentCode) {
        return h3yunService.queryRegionTree(parentCode);
    }

    @ApiOperation(value = "获取有权限的行政区划树")
    @GetMapping("/getPowerRegionTree")
    public Result<JSONObject> getPowerRegionTree(HttpServletRequest request) {
        String userInfoStr = (String) request.getAttribute("userInfo");
        UserInfoDomain userInfo = Objects.requireNonNull(JSON.parseObject(userInfoStr, UserInfoDomain.class));
        return h3yunService.getPowerRegionTree(userInfo.getPowerCode(), userInfo.getUnitId());
    }

    @ApiOperation(value = "获取有权限的单位树")
    @GetMapping("/getPowerOrgTree")
    public Result<JSONObject> getPowerOrgTree(HttpServletRequest request) {
        String userInfoStr = (String) request.getAttribute("userInfo");
        UserInfoDomain userInfo = Objects.requireNonNull(JSON.parseObject(userInfoStr, UserInfoDomain.class));
        return h3yunService.getPowerOrgTree(userInfo.getPowerCode(), userInfo.getUnitId());
    }

    @ApiOperation(value = "获取系统配置", notes = "获取系统配置信息")
    @GetMapping("/powerConfig")
    public Result<JSONObject> getPowerConfig() {
        return h3yunService.getPowerConfig();
    }

    @ApiOperation(value = "获取单站配置", notes = "获取单站配置")
    @GetMapping("/getStationConfig")
    public Result<JSONObject> getStationConfig(String areaCode, HttpServletRequest request) {
        String userInfoStr = (String) request.getAttribute("userInfo");
        UserInfoDomain userInfo = Objects.requireNonNull(JSON.parseObject(userInfoStr, UserInfoDomain.class));
        return h3yunService.getStationConfig(areaCode);
    }

    @ApiOperation(value = "获取主监控点配置列表", notes = "获取主监控点配置列表")
    @GetMapping("/getHomeCameraList")
    public Result<JSONArray> getHomeCameraList(HttpServletRequest request) {
        String userInfoStr = (String) request.getAttribute("userInfo");
        UserInfoDomain userInfo = Objects.requireNonNull(JSON.parseObject(userInfoStr, UserInfoDomain.class));
        return h3yunService.getHomeCameraList(userInfo.getPowerCode(), userInfo.getUnitId());
    }

    @ApiOperation(value = "保存主监控点配置", notes = "保存主监控点配置")
    @PostMapping("/addHomeCamera")
    public Result<JSONObject> addHomeCamera(@RequestBody HomeCameraDomain vo) {
        return h3yunService.addHomeCamera(vo);
    }

    @ApiOperation(value = "删除主监控点配置")
    @GetMapping("/deleteHomeCamera")
    public Result<String> deleteHomeCamera(@ApiParam(value = "ObjectId", required = true) @RequestParam String ObjectId) {
        return h3yunService.deleteHomeCamera(ObjectId);
    }

    @ApiOperation(value = "获取预警事件列表", notes = "获取预警事件列表")
    @GetMapping("/getEventList")
    public Result<JSONArray> getEventList(String areaCode, HttpServletRequest request) {
        String userInfoStr = (String) request.getAttribute("userInfo");
        UserInfoDomain userInfo = Objects.requireNonNull(JSON.parseObject(userInfoStr, UserInfoDomain.class));
        return h3yunService.getEventList(areaCode);
    }

    @ApiOperation(value = "获取工单列表", notes = "获取工单列表")
    @GetMapping("/getWorkOrder")
    public Result<JSONArray> getWorkOrder(String areaCode, HttpServletRequest request) {
        String userInfoStr = (String) request.getAttribute("userInfo");
        UserInfoDomain userInfo = Objects.requireNonNull(JSON.parseObject(userInfoStr, UserInfoDomain.class));
        return h3yunService.getWorkOrder(areaCode);
    }

} 